
import java.util.Scanner;

public class CLI {
    private static Scanner input = new Scanner(System.in);

    //takes a string as argument, and returns another string as result
    private static String readLine(String str) {
        System.out.print(str);
        String str1 = input.nextLine();
        return str1;

    }

    private static int readPosInt(String str) {
        while (true) {
            System.out.print(str);
            if (input.hasNextInt()) {
                int i = 0;
                i = input.nextInt();
                if (i >= 0) {
                    input.nextLine();
                    return i;
                } else {
                    input.nextLine();
                    System.out.println("Positive integers only!");

                }
            } else {
                String s = input.nextLine();
                System.out.println("You must type an integer!");
            }
        }

    }


    public static void main(String[] args) throws NotALenderException, UnknownUserException {
        Library UL = new Library("UIC Library");
        while (true) {
            int i = readPosInt("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6):");
            //using switch-case
            switch (i) {
                case 1:
                    System.out.println("Total number of borrowed books: " + UL.totalBorrowedBooks());
                    break;
                case 2:
                    int a = readPosInt("Type the user role (lender:1 borrower:2): ");
                    switch (a) {
                        case 1:
                            String lenderName = readLine("Enter the name of the user: ");
                            int lendBooks = readPosInt("Enter the initial number of borrowed books: ");
                            Lender l = new Lender(lenderName, lendBooks);
                            UL.addUser(l);
                            System.out.println("Lender " + lenderName + " lending " + lendBooks + " book(s) has been added.");
                            break;
                        case 2:
                            String borrowerName = readLine("Enter the name of the user: ");
                            int borrowBooks = readPosInt("Enter the initial number of borrowed books: ");
                            Borrower b = new Borrower(borrowerName, borrowBooks);
                            UL.addUser(b);
                            System.out.println("Borrower " + borrowerName + " borrowing " + borrowBooks + " book(s) has been added.");
                            break;
                        default:
                            System.out.println("Unknown user role!");
                            break;
                    }
                    break;
                case 3:
                    String borrowedName = readLine("Enter the name of the user: ");
                    try {
                        int book = UL.getBook(borrowedName);
                        System.out.println(borrowedName + " borrows " + book + " book(s).");
                    } catch (UnknownUserException e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 4:
                    String borrowedName4 = readLine("Enter the name of the user: ");
                    int nextInt = readPosInt("Enter the number of books:");
                    try {
                        UL.moreBook(borrowedName4, nextInt);
                    } catch (UnknownUserException e) {
                        System.out.println(e.getMessage());
                    } catch (NotALenderException e) {
                        System.out.println("BUG! This must never happen!");
                        System.exit(1);
                    }
                    break;
                case 5:
                    String borrowedName5 = readLine("Enter the name of the user: ");
                    int nextInt5 = readPosInt("Enter the number of books:");
                    try {
                        UL.moreBook(borrowedName5, -nextInt5);
                    } catch (UnknownUserException |NotALenderException e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 6:
					System.out.println("Goodbye!");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Unknown action!");
            }
        }
    }

}
